@echo off
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

echo Program start
echo ARGS: %*

set unrar_exe="C:\Program Files\WinRAR\WinRAR.exe"

set PROG=%0
for /f "usebackq delims=;" %%I in (`echo %PROG%`) do set PROG=%%~nxI
set start_dir=%CD%

REM option /D: delete original files
if /I x%1==x/d (
  set delete_original=yes
  shift
)

REM now at least two arguments
if "%~2"=="" (
  echo usage: !PROG! DESTINATION SOURCE_LIST 1>&2
  goto end
)

set dest_path=%~1
REM Test destination path and update to absolute path
echo Try chdir to destination path "!dest_path!"...
cd /d !dest_path! 2>nul
if errorlevel 1 (
  echo error: destination path error: !dest_path! 1>&2
  goto end
)
set dest_path=%cd%
set dest_drive=%dest_path:~0,1%
echo ** Dest drive: !dest_drive!
echo *** Full Path: !dest_path!

REM all remaining args are the dirs to extract
for /f %%D in (%2) do (
  call :sub_extract "%%D" "!dest_path!"
)

REM -------------------------------------------------------
:end
goto :EOF

REM -------------------------------------------------------
:sub_extract
set source_path=%~1
set dest_path=%~2
set start_date=%date% %time%
REM Test source path and update to absolute path
echo Try chdir to source path:
cd /d "!source_path!" 2>nul
if errorlevel 1 (
  echo error: source path error: !source_path! 1>&2
  echo the sub-routine will now abort... 1>&2
  goto :eof
)

set source_path=%cd%
for /f "usebackq delims=;" %%I in (`echo !source_path!`) do set rls_name=%%~nxI
set source_drive=%source_path:~0,1%
echo ** Source drive: !source_drive!
echo ** Release name: !rls_name!
set full_dest_path=!dest_path!\!rls_name!
echo ***** Full destination path:
echo !full_dest_path!

REM check whether the destination path exists, if not, just create it
if not exist !full_dest_path! (
  echo Destination directory does not exist, now creating...
  mkdir !full_dest_path!
) else (
  echo Okay, destination directory already exists.
)

REM start operations
echo Operations start at: %date% %time%
for /d %%I in (Sample* Sub* Intro*) do (
  echo Found "sample-or-subs-like" directory: %%I, start to copy...
  echo Moving "%%I" to !full_dest_path!...
  if !source_drive!==!dest_drive! (
    move "%%I" "!full_dest_path!\%%I"
  ) else (
    xcopy /I /S /E /Q /Y "%%I" "!full_dest_path!\%%I"
    if x!delete_original!==xyes rd /S /Q "%%I"
  )
)

for %%F in (*.nfo *.txt *.jpg *.png *.jpeg) do (
  echo Moving file "%%F" to destination...
  move %%F !full_dest_path!
)

for /d %%D in (. CD?) do (
  echo Extracting [%%D]...
  cd %%D
  for %%F in (*.rar) do set rar_file=%%F
  if exist !rar_file! (
    echo RAR File: !rar_file!
    !unrar_exe! x -o+ !rar_file! "!full_dest_path!\"
  )
  if not exist %%D cd ..
)

REM subtitle extraction
cd /d !full_dest_path!
for /d %%S in (sub*) do (
  for %%R in (%%S\*.rar) do !unrar_exe! x -o+ %%R
  REM remove .sfv/rushchk.log files
  del /q /f %%S\*.sfv %%S\rushchk.*
  REM remove informational directories
  for /d %%d in (%%S\[*]) do rmdir /s /q "%%d"
)

REM current extraction done
echo DoNE.
echo Start: !start_date!
echo Stop:: %date% %time%
goto :eof

:: vim: filetype=dosbatch tabstop=2 expandtab shiftwidth=2
